package com.es.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author zqf
 * @Date 2022/10/14 15:45
 * @Description: 删除文档
 */
@RestController("/delete")
@Api(tags = "删除文档")
public class DeleteDocController {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Value("${elasticsearch.index}")
    private String esIndex;



    @GetMapping("/byId")
    @ApiOperation("根据id删除数据")
    public String testDeleteDocument(@RequestParam("id") @ApiParam("id") String id) throws IOException {
        DeleteRequest request = new DeleteRequest(esIndex, id);
        request.timeout("1s");
        DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        if (RestStatus.OK.equals(response.status())){
            return "删除成功";
        }
        return "删除成功";
    }

}
